//
//  DCHUINavigationController.m
//  DCH-BaiSiBuDeJie
//
//  Created by apple on 16/10/22.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

#import "DCHUINavigationController.h"
#import "DCHUINavigationBar.h"
#import "DCHBackView.h"

@interface DCHUINavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation DCHUINavigationController



+ (void)load
{


    
    
    
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    // 设置导航条标题字体 => 导航条
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    bar.titleTextAttributes = attr;
    
    // 设置导航条背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
}

#pragma mark -UIGestureRecognizerDelegate
//控制手势是否触发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count >1;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
   
    //不要系统的手势
    self.interactivePopGestureRecognizer.enabled =NO;

    id target =self.interactivePopGestureRecognizer.delegate;
    //创建手势
    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:target action:@selector(handleNavigationTransition:)];
    pan.delegate =self;
    [self.view addGestureRecognizer:pan];
    
    //创建自己导航条
    DCHUINavigationBar *bar1 =[[DCHUINavigationBar alloc]initWithFrame:self.navigationBar.frame];
    
    [self setValue:bar1 forKey:@"navigationBar"];

   
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated


{
    
    if (self.childViewControllers.count >0) {
        
         DCHBackView *backView =[DCHBackView backViewWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"] target:self action:@selector(back) title:@"返回"];
        
        //设置返回按钮
         viewController.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:backView];
        
        //一定要在push之前设置
        viewController.hidesBottomBarWhenPushed =YES;
    }
   

       //真正push
    [super pushViewController:viewController animated:animated];
}
- (void)back
{
    
    [self popViewControllerAnimated:YES];
    
}


@end
